package com.example.startproject_206shj;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.startproject_206shj.bean.WeatherInfo;
import com.example.startproject_206shj.iface.WeatherListenser;
import com.example.startproject_206shj.model.Weather51Model;
import com.example.startproject_206shj.model.WeatherModel;

public class MainActivity extends AppCompatActivity implements WeatherListenser,View.OnClickListener{

    private EditText etcity;
    private Button btnfind1, btnfind2;
    private TextView tvcityname, tvtemp, tvsd, tvws, tvwd;
    private Spinner spinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init(){
        etcity = (EditText) findViewById(R.id.editText);
        btnfind1 = (Button) findViewById(R.id.button);
        btnfind2 = (Button) findViewById(R.id.button2);
        tvcityname = (TextView)findViewById(R.id.textView_city);
        tvtemp = (TextView)findViewById(R.id.textView_temp);
        tvsd = (TextView)findViewById(R.id.textView_SD);
        tvws = (TextView)findViewById(R.id.textView_WS);
        tvwd = (TextView)findViewById(R.id.textView_WD);
        btnfind1.setOnClickListener(this);
        btnfind2.setOnClickListener(this);
        spinner=(Spinner) findViewById(R.id.spinner);
        final String arr[]=new String[]{
                "大连",
                "北京",
                "上海"
        };
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arr);
        spinner.setAdapter(arrayAdapter);
        //Toast.makeText(getApplicationContext(), "main Thread"+spinner.getItemIdAtPosition(spinner.getSelectedItemPosition()), Toast.LENGTH_LONG).show();
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                Spinner spinner=(Spinner) parent;
                Toast.makeText(getApplicationContext(), "" + spinner.getItemAtPosition(position), Toast.LENGTH_LONG).show();
                switch (position){
                    case 0:
                        etcity.setText("101070201");
                        break;
                    case 1:
                        etcity.setText("101010100");
                        break;
                    case 2:
                        etcity.setText("101020100");
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                Toast.makeText(getApplicationContext(), "Nothing Selected!", Toast.LENGTH_LONG).show();
            }

        });
    }

    @Override
    public void onResponse(WeatherInfo weatherInfo) {
        if(weatherInfo != null){
            tvcityname.setText("城市： " + weatherInfo.getCity());
            tvtemp.setText("温度： " + weatherInfo.getTemp());
            tvsd.setText("湿度： " + weatherInfo.getSD());
            tvws.setText("风力： " + weatherInfo.getWS());
            tvwd.setText("风向： " + weatherInfo.getWD());
            //课下自行添加其他的天气信息并设置文本
        }else{
            tvcityname.setText("城市： 未知");
            tvtemp.setText("温度：未知");
            tvsd.setText("湿度：未知");
            tvws.setText("风力： 未知");
            tvwd.setText("风向： 未知");
        }
    }

    @Override
    public void onFail(String msg) {
        tvcityname.setText(msg);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button://中国天气网
                WeatherModel weatherModel = new WeatherModel();
                weatherModel.getWeather(etcity.getText().toString(), this);
                break;
            case R.id.button2://51万年历天气
                Weather51Model weather51Model = new Weather51Model();
                weather51Model.getWeather(etcity.getText().toString(), this);
                break;
        }
    }
}
